﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
*/
var delimeter = ';';
var emptyBasketText;

function addToBasket(productId) {
    $('#basket-content').css('text-decoration', 'line-through');
    $.ajax({
        type: "POST",
        url: "BasketMngr.ashx",
        data: { 'ProductId': productId, 'Operation': 1 },
        success: resultAddToBasket
    });
}

function resultAddToBasket(msg) {

    var result = msg.split(delimeter);
    //jeśli element istnieje to zmianimy wpis
    if (parseInt(result[4]) > 1) {
        $('#basket-content div').filter('[@id=pid' + result[0] + ']')[0].innerHTML = '<span>' + result[1] + '(x' + result[4] + ')</span><a href="#" onClick="removeFromBasket(' + result[0] + '); return false;">' + translate('DeleteLower') + '</a>';
    }
    else //jeśli nowy to dodajemy
    {
        //ewentulane usunięcie wpisu o pustym koszyku
        $('#empty-basket').remove();
        $('#basket-content').append('<div id="pid' + result[0] + '"><span>' + result[1] + '</span><a href="#" onClick="removeFromBasket(' + result[0] + '); return false;">' + translate('DeleteLower') + '</a></div>');
    }
    $('#basket-content').css('text-decoration', '');
    //ustawiamy cenę
    ChangeSumPriceSmallBasket(result[5]);

}

function removeFromBasket(productId) {
    $('#basket-content').css('text-decoration', 'line-through');
    $.ajax({
        type: "POST",
        url: "BasketMngr.ashx",
        data: { 'ProductId': productId, 'Operation': 2 },
        success: resultRemoveFromBasket
    });
}



function resultRemoveFromBasket(msg) {

    var result = msg.split(delimeter);
    $('#basket-content').css('text-decoration', '');
    //usuniecie wpisu
    RemoveFromSmallBasket(result[0]);
    //zmiana całkowitej ceny
    ChangeSumPriceSmallBasket(result[4]);
    //dodanie wpisu o tym że koszyk jest pusty o ile potrzeba
    if (result[3] == 0) {
        $('#basket-content').append('<div id="empty-basket"><span>' + emptyBasketText + '</span></div>');
    }
    //usunięcie z dużego koszyka, jeśli jest otwarty
    if ($('#sumAllPrice span')[1] != null) {
        var control = $('#lnkRemove' + result[0])[0];
        control = control.parentNode.parentNode;
        control.parentNode.removeChild(control);
        ChangeSumAllPrice(result[4], result[5]);
        //------------------------------------------------
    }


}

function ChangeAmount(obj, productId) {
    obj.disabled = true;
    $.ajax({
        type: "POST",
        url: "BasketMngr.ashx",
        data: { 'ControlName': obj.id, 'Operation': 3, 'ProductID': productId, 'Count': obj.value },
        success: ChangeAmountResult
    });
}

function ChangeAmountResult(msg) {

    var result = msg.split(delimeter);
    var control = $('#' + result[7])[0];
    if (result[2] > 0) {
        control.disabled = false;
        control = control.parentNode.parentNode; // TR
        //szukamy miejsca gdzie wpisać nową cenę
        for (i = 0; i < control.childNodes.length; i++) {
            if (control.childNodes[i].nodeType == 1 && control.childNodes[i].id.indexOf("sumPrice") != -1) {
                control = control.childNodes[i];
                break;
            }
        }
        //ustawienie ceny w tabeli
        //control.innerHTML = result[3];
        control.innerHTML = result[3] + ' ' + ' (' + result[4] + ' ' + translate('WithoutVat') + ')';
        //ustawienie ilości w bocznym koszyku
        $('#basket-content div').filter('[@id=pid' + result[0] + ']')[0].innerHTML = '<span>' + result[1] + '(x' + result[2] + ')</span><a href="#" onClick="removeFromBasket(' + result[0] + '); return false;">' + translate('DeleteLower') + '</a>';

    }
    else {
        //usunięcie wpisu w tabeli
        control = control.parentNode.parentNode;
        //-------------------------------------------
        control.parentNode.removeChild(control);
        //-------------------------------------------
        //usuniecie wpisu w bocznym koszyku
        RemoveFromSmallBasket(result[0]);
    }

    //ustawienie sumy ceny produktów
    ChangeSumAllPrice(result[5], result[6]);

    //ustawienie sumy ceny produktów w bocznym koszyku
    ChangeSumPriceSmallBasket(result[5]);
}

function removeFromBasketOnGrid(obj, productId) {
    $.ajax({
        type: "POST",
        url: "BasketMngr.ashx",
        data: { 'ProductId': productId, 'Operation': 2, 'ControlName': obj.id },
        success: resultRemoveFromBasketOnGrid
    });
}

function resultRemoveFromBasketOnGrid(msg) {
    var result = msg.split(delimeter);
    var control = $('#' + result[6])[0];
    //usunięcie wpisu w tabeli
    //-------------------------------------------    
    control = control.parentNode.parentNode;
    control.parentNode.removeChild(control);
    //-------------------------------------------

    RemoveFromSmallBasket(result[0]);

    ChangeSumPriceSmallBasket(result[4]);
    ChangeSumAllPrice(result[4], result[5]);
}

function RemoveFromSmallBasket(productId) {
    $('#basket-content div').filter('[@id=pid' + productId + ']').remove();
}

function ChangeSumPriceSmallBasket(newPrice) {
    //$('#basket-sumprice span')[1].innerHTML = newPrice + translate('Currency');
    $('#basket-sumprice span')[1].innerHTML = newPrice + ' ' + translate('Currency'); // ('+newPriceNumber.toFixed(2) + ' zł bez VAT)';
}

function ChangeSumAllPrice(newPriceBrutto, newPriceNetto) {
    if (newPriceBrutto == '0,00') {
        ChangeHyperlinkToBack();
        $('#grid').remove();
    }
    control = $('#sumAllPrice span');
    var newPriceNumberBrutto = parseFloat(newPriceBrutto.replace(',', '.'));
    var newPriceNumberNetto = parseFloat(newPriceNetto.replace(',', '.'));
    //control[1].innerHTML = newPriceNumberBrutto.toFixed(2) + ' ' + translate('Currency') + '(' + newPriceNumberNetto.toFixed(2) + ' ' + translate('WithoutVat') + ')';
    control[1].innerHTML = newPriceBrutto + ' ' + translate('Currency') + ' (' + newPriceNetto + ' ' + translate('WithoutVat') + ')';
    //control[1].innerHTML = (newPrice + ' ' + translate('Currency') + '(' + newPriceNumber.toFixed(2) + ' ' + translate('WithoutVat') + ')';
    // jeśli nowa cena będzie równa 0 zamienia link "Do kasy" na "Powrót"

}

function ChangeHyperlinkToBack() {
    hypLinks = $('#navigate a');
    hypLinks[0].innerHTML = translate('Return');
    hypLinks[0].setAttribute('href', hypLinks[1].getAttribute('href'));

}

function clearBasket() {
    $.ajax({
        type: "POST",
        url: "BasketMngr.ashx",
        data: { 'ProductId': -1, 'Operation': 4 },
        success: resultClearBasket
    });
}

function resultClearBasket(msg) {
    var newPriceNumber = parseFloat(0.0);
    $('#basket-sumprice span')[1].innerHTML = (newPriceNumber.toFixed(2)) + ' ' + translate('Currency');
    $('#basket-content div').remove();
    $('#basket-content').append('<div id="empty-basket"><span>' + emptyBasketText + '</span></div>');
    if ($('#sumAllPrice span')[1] != null)//jeśli otwarty 'duży koszyk'
    {
        $('#grid div').remove();
        $('#sumAllPrice span')[1].innerHTML = (newPriceNumber.toFixed(2)) + ' ' + translate('Currency');
        ChangeHyperlinkToBack();
    }
}

function StopEnterPostBack(obj, e) {
    e = e ? e : event;
    if (e && e.keyCode == 13) {
        obj.blur();
        return false;
    }
}

function SetEmptyBasketText(value) {
    emptyBasketText = value;
}